#include "precompiled.h"
#include "Util.h"
#include "Config.h"

bool   Config::Debug				= false;

json::grammar<char>::object Config::root;

void Config::Load(const char *filename) {
	using namespace json;
	using namespace boost;

	char *rawinput = Util::readEntireFile(filename);
	std::string input(rawinput);
	delete[] rawinput;

	int begin = input.find_first_of('{');
	int end = input.find_last_of('}');
	
	grammar<char>::variant v = parse(input.begin() + begin, input.begin() + end + 1);

	if(v->type() != typeid(json::grammar<char>::object)){
		// ERROR: parsing failed (bad format)
		std::ostringstream msg;
		msg << "failed to parse JSON: " << input;
		throw std::runtime_error(msg.str());
	}

	root = boost::any_cast<grammar<char>::object>(*v);

	TryAssign(Debug, "Debug", root);
}